import {Injectable} from '@angular/core';
import {Headers, Http} from '@angular/http';
import {ParamConfig} from '../common/param.config';
import 'rxjs/add/operator/toPromise';
import {AddEventPage, AddEvent} from './event';
import {Router} from '@angular/router';
import {UserService} from '../user/user.service';

@Injectable()
export class AddEventService {
  public token: string;
  private hostUrl = ParamConfig.HTTP_WEB_HOST_URL + 'event/getWebEventData'; // 查询事件
  private hostUrlAddEvent = ParamConfig.HTTP_WEB_HOST_URL + 'event/saveEvent'; // 新增事件
  private hostUrlDeleteEvent = ParamConfig.HTTP_WEB_HOST_URL + 'event/deleteEvent'; // 删除事件
  public id: string;

  constructor(private http: Http, private userService: UserService, private router: Router) {
  };

// 查询事件
  getEvent(page): Promise<any> {
    this.token = this.userService.getToken();
    const request = JSON.stringify({
      rows: ParamConfig.DATA_LIST_PAGE_SIZE,
      page: page,
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrl, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            const pagedata = new AddEventPage();
            pagedata.addEvent = body as AddEvent[];
            pagedata.totalPages = body.total;
            return pagedata;
          }
          if (body.status === 2) {
            alert(body.msg);
            this.router.navigate(['/login']);
          }
        }
      )
      .catch(this.handleError);
  }

  // 增加事件

  addEventinfo(addEvent: AddEvent, fileId): Promise<any> {
    this.token = this.userService.getToken();
    const request = JSON.stringify({
      name: addEvent.name,
      type: addEvent.type,
      fileId: fileId,
      sort: addEvent.sort,
      status: addEvent.status
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrlAddEvent, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            return true;
          }
          if (body.status === 2) {
            alert(body.msg);
            this.router.navigate(['/login']);
          }
        }
      )
      .catch(this.handleError);
  }

  // 获取编号
  getEventId(addEvent: AddEvent) {
    this.id = addEvent.id;
  }

  sendEventId() {
    return this.id;
  }

// 删除事件
  removeEventinfo(addEvent: AddEvent): Promise<any> {
    const request = JSON.stringify({
      eventId: addEvent.id
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrlDeleteEvent, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            return true;
          }
          if (body.status === 2) {
            alert(body.msg);
            this.router.navigate(['/login']);
          }
        }
      )
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<[boolean, any]> {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject([false, error.toString()]);
  }
}
